import axios from 'axios'
export default{
    namespaced:true,
    state:{
        shopcartList:[]
    },
    mutations:{
        setShopCartList(state,payload){
           console.log('--------mutatations的setShopCartList----------');
           console.log('payload',payload);
           state.shopcartList=payload
        }
    },
    actions:{
        async getShopCartList(cxt,payload){
           console.log('--------actions的getShopCartList----------');
           const result= await axios({
               url:'http://www.zhaijizhe.cn:3003/shopcart/getShopcartData' 
           })
           cxt.commit('setShopCartList',result.data.data)
        },
        async changeNumAsync(cxt,payload){
            console.log('--------actions的changeNumAsync----------');
            const result=await axios.post('http://www.zhaijizhe.cn:3003/shopcart/changeNum',payload)
            if(result.data.code){
                alert(result.data.message)
                cxt.dispatch("getShopCartList")
            }
        }
    },
    getters:{
        getTotal(state){
            return state.shopcartList.filter(item=>item.checked).reduce((prev,cur)=>prev+cur.price*cur.num,0)
        }
    }
}